package com.car.carcontrol;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.car.carcontrol.base.BaseFragment;
import com.car.carcontrol.util.SharedPreferencesUrls;
import com.car.carcontrol.util.Utils;
import com.google.gson.Gson;

import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;

//信息
public class Fragment_Eighth extends BaseFragment {
    private FrameLayout v;
    private Context context;

    private GifImageView iv_route;
    private GifDrawable gifDrawable;

    private long loopTime = 1000;  //时间显示间隔   默认100毫秒
    private boolean isThread = true;
    Thread thread;

    public Object[] allParam = new Object[]{
            new Object[]{"iv", new Rect(609*width/1280-width_d, 334*height/480-height_d, 145*width/1280, 152*height/480), R.drawable.light1, R.drawable.light1f, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}}
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = new FrameLayout(getActivity());

        context = v.getContext();

//        String s = "";
//        try {
////            String s0 = "[\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":340,\\\\\\\"left\\\\\\\":0,\\\\\\\"right\\\\\\\":984,\\\\\\\"top\\\\\\\":0}\\\",2131165295,0,false,\\\"[6,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":95,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":10}\\\",2131165449,2131165450,true,\\\"[6,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":427,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":10}\\\",2131165455,2131165456,true,\\\"[9,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":760,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":10}\\\",2131165390,2131165391,true,\\\"[16,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":95,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":180}\\\",2131165447,2131165448,true,\\\"[5,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":427,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":180}\\\",2131165449,2131165450,true,\\\"[6,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":760,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":180}\\\",2131165392,2131165393,true,\\\"[17,1,2]\\\"]\"]";
//            String s0 = "[\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":340,\\\\\\\"left\\\\\\\":0,\\\\\\\"right\\\\\\\":984,\\\\\\\"top\\\\\\\":0}\\\",2131165295,0,false,\\\"[6,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":95,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":10}\\\",2131165449,2131165450,true,\\\"[6,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":427,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":10}\\\",2131165455,2131165456,true,\\\"[9,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":760,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":10}\\\",2131165390,2131165391,true,\\\"[16,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":95,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":180}\\\",2131165447,2131165448,true,\\\"[5,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":427,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":180}\\\",2131165449,2131165450,true,\\\"[6,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":760,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":180}\\\",2131165392,2131165393,true,\\\"[17,1,2]\\\"]\"]";
//
////            String s = compress(s0);
////            String s = bytesToString(compress("[\"[\\\"iv\\\",\\\"{\\\\\\\"bottom"));
////            String s = bytesToString(compress("中国Chinaeaerfupuiferweff"));
//            s = Utils.bytesToString(Utils.compress(s0)).replace(" ", "");
////            String s = compress(" [\"[\\\"iv\\\",\\\"{\\\\\\\"bottom");
//
////            50 4B 03 04 14 00 08 08 08 00 9C 32 86 50 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 30 7B B2 63 ED D3 D9 7B 9D 33 32 F3 12 01 50 4B 07 08 EA DC 06 93 0D 00 00 00 0B 00 00 00
////            50 4B 03 04 14 00 08 08 08 00 FC 32 86 50 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 30 7B B2 63 ED D3 D9 7B 9D 33 32 F3 12 9F 20 98 00 50 4B 07 08 96 82 A4 C1 10 00 00 00 16 00 00 00
////            50 4B 03 04 14 00 08 08 08 00 22 33 86 50 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 30 7B B2 63 ED D3 D9 7B 9D 33 32 F3 12 53 13 53 8B D2 4A 0B 4A 33 D3 52 8B CA 53 D3 D2 00 50 4B 07 08 33 2C 3D 14 1D 00 00 00 1B 00 00 00
//
////            s=""
//
//
//            Log.e("ma===onCreate1", width+"==="+height+"==="+density+"==="+s0.length()+"==="+s.length()+"==="+s+"==="+Utils.decompress(Utils.hexStringToByteArray(s)).length());
////            Log.e("ma===onCreate1", width+"==="+height+"==="+density+"==="+s+"==="+decompress(hexStringToByteArray(s)));
//        } catch (Exception e) {
//            Log.e("ma===onCreate==e", e+"===");
//            e.printStackTrace();
//        }
//
//        String json = Utils.decompress(Utils.hexStringToByteArray(s));
        String json = SharedPreferencesUrls.getInstance().getString("allParam8", "");

        Log.e("fragment_Eighth===0", width+"==="+height+"==="+allParam);

        if(json==null || "".equals(json)){
            allParam = new Object[]{
                    new Object[]{"iv", new Rect(609*width/1280-width_d, 334*height/480-height_d, 145*width/1280, 152*height/480), R.drawable.light1, R.drawable.light1f, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}}
            };
        }else{
            Gson gson = new Gson();
            allParam = gson.fromJson(json, Object[].class);

            Log.e("fragment_Eighth===1", width+"==="+height+"==="+allParam.length+"==="+allParam[0]);

            initView();

            isThread = true;
            loopRead();
        }

        return v;
    }

    @Override
    public void onDestroy() {
        Log.e("f8===onDestroy", "===");

        isThread=false;

        if(thread!=null){
            thread.interrupt();
        }

        super.onDestroy();
    }

    private void initView(){
        for (int i=0; i<allParam.length; i++){
            Gson gson = new Gson();
            Object[] obj = gson.fromJson((String)allParam[i], Object[].class);

            Log.e("f8===obj", obj.length+"==="+obj);

            Rect rect = gson.fromJson((String)obj[1], Rect.class);

            String type = (String)obj[0];

            Log.e("f8===obj1", type+"==="+rect);

            if("tv".equals(type)){
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                String name = (String)obj[2];
                int textSize = ((Double)obj[3]).intValue();
                int textColor = ((Double)obj[4]).intValue();
                int textBcg = ((Double)obj[7]).intValue();
                TextView tv = new TextView(context);
                tv.setText(name);
                tv.setTextSize(textSize);
                tv.setTextColor(textColor);
                tv.setBackgroundResource(textBcg);
                tv.setPadding(20*width/1280, 10*height/480, 20*width/1280, 10*height/480);
                v.addView(tv, params);

                boolean isCanClick = (boolean)obj[5];

                if(isCanClick){
                    tv.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Log.e("onClick===", v+"===");

                            String name = (String)obj[2];
//                            byte[] bytes = (byte[])obj[4];

//                            write(bytes); //TODO
                        }
                    });

                    tv.setOnTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {


                            return false;
                        }
                    });
                }
            }else if("gif".equals(type)){
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(rect.right, rect.bottom);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                int imgSrc = (int)obj[2];
                iv_route = new GifImageView(context);
                iv_route.setImageResource(imgSrc);
                iv_route.setScaleType(ImageView.ScaleType.FIT_XY);
                gifDrawable = (GifDrawable) iv_route.getDrawable();
                gifDrawable.stop();

                v.addView(iv_route, params);
            }else if("iv".equals(type)){
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(rect.right, rect.bottom);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                int imgSrc = ((Double)obj[2]).intValue();
                int imgSrc2 = ((Double)obj[3]).intValue();
                boolean isCanClick = (boolean)obj[4];
                byte[] bytes = gson.fromJson((String)obj[5], byte[].class);

                ImageView iv = new ImageView(context);
                iv.setImageResource(imgSrc);
                iv.setScaleType(ImageView.ScaleType.FIT_XY);
                v.addView(iv, params);

                if(isCanClick){
                    iv.setOnTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            Log.e("onTouch===", v+"==="+event.getAction()+"==="+bytes[0]+"==="+bytes[1]+"==="+bytes[2]);

                            if(event.getAction()==1){
                                iv.setImageResource(imgSrc);
                            }else{
                                iv.setImageResource(imgSrc2);
                            }

//                            write(bytes); //TODO

                            return true;
                        }
                    });

                }
            }
        }
    }

    private void loopRead(){
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (isThread){
                    try {
                        Thread.sleep(loopTime);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    if(MainActivity.curPos==7){
                        m_myHandler.sendEmptyMessage(1);
                    }
                }

            }
        });
        thread.start();
    }

    protected Handler m_myHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message mes) {
            switch (mes.what) {
                case 0:
                    break;

                case 1:
//                    read(cmd_needle); //TODO

//                    Log.e("f8===", "===");

                    break;


                default:
                    break;
            }
            return false;
        }
    });

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

}

